import React, { FC } from 'react';
import { Button, message } from 'antd';
import { useLocation, useNavigate } from 'react-router-dom';
import { type FormPanelObject } from 'lfun-bg-comp';
import Edit from './edit';
import CloseReson from '../component/closeReson';
import { ClosePlanningSchool, closePlanningSchool } from '@/service/unit/planning';
import { getPathRoot } from '@/utils';

const Groups: FC = () => {
  const navigate = useNavigate();
  const [showCloseResone, setShowCloseResone] = React.useState(false);
  const location = useLocation();
  const [edit, setEdit] = React.useState(true);

  return (
    <>
      <CloseReson
        show={showCloseResone}
        onCancel={form => {
          form.resetFields();
          setShowCloseResone(false);
        }}
        onOk={async (formPanelObject: FormPanelObject) => {
          const values = formPanelObject.getFieldsValue();
          const closeParams: ClosePlanningSchool = {
            planning_school_id: location.state.id,
            ...values,
          };
          const res = await closePlanningSchool(closeParams);
          if (res.success) {
            message.success('关闭成功');
            setShowCloseResone(false);
            //search(form?.getFieldsValue(), pageSize, pageIndex);
            formPanelObject.resetFields();
            navigate(`${getPathRoot()}/planning`);
          } else {
            formPanelObject.reVaildation(res);
          }
        }}
      />

      <Edit
        isEdit={edit}
        buttonList={[
          <Button
            onClick={() => {
              navigate(`${getPathRoot()}/planning`);
            }}
          >
            返回
          </Button>,
          <Button style={{ display: 'inline' }} type="primary" onClick={() => setEdit(true)}>
            变更
          </Button>,
          <Button onClick={() => setShowCloseResone(true)}>关闭</Button>,
        ]}
      />
    </>
  );
};

export default Groups;
